home *** CD-ROM | disk | FTP | other *** search
/ PLAYymate for OS/2 / Playmate for OS2.iso / p4os2008 / ckrboard.c < prev    next >
C/C++ Source or Header  |  1990-09-01  |  13KB  |  348 lines

  1. /*---------------------------------------------------------------
  2.    CKRBOARD.C -- BoardWndProc for user interaction, Version 0.40
  3.                  (c) 1990, Charles Petzold
  4.   ---------------------------------------------------------------*/
  5.  
  6. #define INCL_WIN
  7. #include <os2.h>
  8. #include <stdlib.h>
  9. #include "checkers.h"
  10. #include "ckrdraw.h"
  11.  
  12. #define TIMER_ID      1
  13. #define TIMER_STEPS  10
  14. #define TIMER_TIME  100
  15.  
  16. extern HAB hab ;
  17.  
  18. MRESULT EXPENTRY BoardWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  19.      {
  20.      static BOOL     fMovingPiece ;
  21.      static HPS      hps ;
  22.      static HPOINTER hptr, hptrUpHand, hptrDnHand, hptrArrow, hptrWait ;
  23.      static HWND     hwndJudge ;
  24.      static POINTL   ptlMouse, ptlLast ;
  25.      static SHORT    sBottom = BLACK, sColor = -1, sKing = 0,
  26.                      sTimerBeg, sTimerEnd, sTimerColor, sTimerKing, sTimerInc ;
  27.      BOARD           brd ;
  28.      NEWGAME         *pnewgame ;
  29.      SHORT           x, y, i ;
  30.  
  31.      switch (msg)
  32.           {
  33.           case WM_CREATE:
  34.                hps = CkdCreatePS (hwnd) ;
  35.  
  36.                hptrUpHand = WinLoadPointer (HWND_DESKTOP, 0, IDP_UPHAND) ;
  37.                hptrDnHand = WinLoadPointer (HWND_DESKTOP, 0, IDP_DNHAND) ;
  38.                hptrArrow  = WinQuerySysPointer (HWND_DESKTOP, SPTR_ARROW,
  39.                                                 FALSE) ;
  40.                hptrWait   = WinQuerySysPointer (HWND_DESKTOP, SPTR_WAIT,
  41.                                                 FALSE) ;
  42.                return 0 ;
  43.  
  44.           case WM_SIZE:
  45.                CkdResizePS (hps, hwnd) ;
  46.                CkdDestroyPieces () ;
  47.                CkdCreatePieces (hps) ;
  48.                return 0 ;
  49.  
  50.           case WM_NEW_GAME:
  51.                pnewgame  = PVOIDFROMMP (mp1) ;
  52.                hwndJudge = pnewgame->hwndJudge ;
  53.                sBottom   = pnewgame->sBottom ;
  54.  
  55.                fMovingPiece = FALSE ;
  56.                sColor       = -1 ;
  57.                hptr         = hptrArrow ;
  58.  
  59.                WinInvalidateRect (hwnd, NULL, FALSE) ;
  60.                return 0 ;
  61.  
  62.           case WM_JUDGE_SAYS_MAKE_MOVE:
  63.                sColor = SHORT1FROMMP (mp2) ;
  64.                hptr   = (sBottom == sColor ? hptrUpHand : hptrDnHand) ;
  65.  
  66.                WinSetPointer (HWND_DESKTOP, hptr) ;
  67.                return 0 ;
  68.  
  69.           case WM_JUDGE_SAYS_SHOW_HOURGLASS:
  70.                WinSetPointer (HWND_DESKTOP, hptr = hptrWait) ;
  71.                return 0 ;
  72.  
  73.           case WM_JUDGE_SAYS_SHOW_ARROW:
  74.                WinSetPointer (HWND_DESKTOP, hptr = hptrArrow) ;
  75.                return 0 ;
  76.  
  77.           case WM_BUTTON1DOWN:          // save mouse coordinates
  78.  
  79.                ptlMouse.x = MOUSEMSG(&msg)->x ;
  80.                ptlMouse.y = MOUSEMSG(&msg)->y ;
  81.                break ;
  82.  
  83.           case WM_BUTTON1UP:
  84.                if (sColor == -1)
  85.                     return 0 ;
  86.                                         // get index from saved mouse coords
  87.  
  88.                CkdQueryHitCoords (hps, ptlMouse, &x, &y) ;
  89.                i = CkdConvertCoordsToIndex (x, y, sBottom) ;
  90.  
  91.                if (i == -1)             // didn't hit black square
  92.                     {
  93.                     WinAlarm (HWND_DESKTOP, WA_ERROR) ;
  94.                     return 0 ;
  95.                     }
  96.  
  97.                if (!fMovingPiece)       // ie, picking up piece
  98.                     {
  99.                     if (!WinSendMsg (hwndJudge, WM_QUERY_JUDGE_PICKUP_PIECE,
  100.                                      MPFROMSHORT (i), NULL))
  101.                          {
  102.                          WinAlarm (HWND_DESKTOP, WA_ERROR) ;
  103.                          return 0 ;
  104.                          }
  105.  
  106.                     sKing = SHORT1FROMMR (
  107.                               WinSendMsg (hwndJudge,
  108.                                           WM_QUERY_JUDGE_IF_KING, NULL, NULL));
  109.  
  110.                               // Remove the mouse pointer
  111.  
  112.                     WinSetPointer (HWND_DESKTOP, hptr = NULL) ;
  113.  
  114.                               // Erase, save area, and show piece at mouse
  115.  
  116.                     CkdErasePiece (hps, x, y) ;
  117.                     CkdDragSave (hps, &ptlMouse, sKing) ;
  118.                     CkdDragShow (hps, &ptlMouse, sColor, sKing) ;
  119.  
  120.                               // Prepare for WM_MOUSEMOVE
  121.  
  122.                     fMovingPiece = TRUE ;
  123.                     ptlLast = ptlMouse ;
  124.                     }
  125.  
  126.                else           // ie, attempt to set down piece
  127.                     {
  128.                     if (!WinSendMsg (hwndJudge, WM_QUERY_JUDGE_PUTDOWN_PIECE,
  129.                                      MPFROMSHORT (i), NULL))
  130.                          {
  131.                          WinAlarm (HWND_DESKTOP, WA_ERROR) ;
  132.                          return 0 ;
  133.                          }
  134.                               // restore area
  135.  
  136.                     CkdDragRestore (hps, &ptlMouse, sKing) ;
  137.  
  138.                     sKing = SHORT1FROMMR (
  139.                               WinSendMsg (hwndJudge,
  140.                                           WM_QUERY_JUDGE_IF_KING, NULL, NULL));
  141.  
  142.                               // set down the piece on the square
  143.  
  144.                     CkdDragDeposit (hps, x, y, sColor, sKing) ;
  145.  
  146.                               // check for continued jumps
  147.  
  148.                     if (WinSendMsg (hwndJudge, WM_QUERY_JUDGE_CONTINUE_MOVE,
  149.                                     MPFROMSHORT (i), NULL))
  150.                          {
  151.                          CkdErasePiece (hps, x, y) ;
  152.                          CkdDragSave (hps, &ptlLast, sKing) ;
  153.                          }
  154.                     else           // the move is over
  155.                          {
  156.                          fMovingPiece = FALSE ;
  157.                          sColor       = -1 ;
  158.  
  159.                          WinSetPointer (HWND_DESKTOP, hptr = hptrArrow) ;
  160.                          WinSendMsg (hwndJudge, WM_TELL_JUDGE_BOARD_MOVE_ENDED,
  161.                                      NULL, NULL) ;
  162.                          }
  163.                     }
  164.  
  165.                return 0 ;
  166.  
  167.           case WM_MOUSEMOVE:
  168.                WinSetPointer (HWND_DESKTOP, hptr) ;
  169.  
  170.                               // move the piece
  171.  
  172.                if (fMovingPiece)
  173.                     {
  174.                     ptlMouse.x = MOUSEMSG(&msg)->x ;
  175.                     ptlMouse.y = MOUSEMSG(&msg)->y ;
  176.  
  177.                     CkdDragMove (hps, &ptlLast, &ptlMouse, sColor, sKing) ;
  178.                     ptlLast = ptlMouse ;
  179.                     }
  180.  
  181.                return 0 ;
  182.  
  183.           case WM_CHAR:
  184.                if (CHARMSG(&msg)->fs & KC_KEYUP)
  185.                     return 0 ;
  186.  
  187.                if (!(CHARMSG(&msg)->fs & KC_VIRTUALKEY))
  188.                     return 0 ;
  189.  
  190.                          // convert pointer position to x, y coords
  191.  
  192.                WinQueryPointerPos (HWND_DESKTOP, &ptlMouse) ;
  193.                WinMapWindowPoints (HWND_DESKTOP, hwnd, &ptlMouse, 1) ;
  194.                CkdQueryNearestXYFromPoint (hps, &ptlMouse, &x, &y) ;
  195.  
  196.                          // move the coordinates
  197.  
  198.                switch (CHARMSG(&msg)->vkey)
  199.                     {
  200.                     case VK_HOME:      x = 0 ;  y = 7 ;  break ;
  201.                     case VK_END:       x = 0 ;  y = 0 ;  break ;
  202.                     case VK_PAGEUP:    x = 7 ;  y = 7 ;  break ;
  203.                     case VK_PAGEDOWN:  x = 7 ;  y = 0 ;  break ;
  204.  
  205.                     case VK_UP:        y = min (y + 1, 7) ;  break ;
  206.                     case VK_DOWN:      y = max (y - 1, 0) ;  break ;
  207.                     case VK_RIGHT:     x = min (x + 1, 7) ;  break ;
  208.                     case VK_LEFT:      x = max (x - 1, 0) ;  break ;
  209.  
  210.                     case VK_SPACE:     break ;
  211.  
  212.                     default:           return 0 ;
  213.                     }
  214.                          // process keystrokes like mouse messages
  215.  
  216.                CkdQuerySlightOffsetFromXY (hps, x, y, &ptlMouse) ;
  217.  
  218.                switch (CHARMSG(&msg)->vkey)
  219.                     {
  220.                     case VK_SPACE:
  221.                          WinSendMsg (hwnd, WM_BUTTON1DOWN,
  222.                                      MPFROM2SHORT ((SHORT) ptlMouse.x,
  223.                                                    (SHORT) ptlMouse.y), NULL) ;
  224.  
  225.                          WinSendMsg (hwnd, WM_BUTTON1UP,
  226.                                      MPFROM2SHORT ((SHORT) ptlMouse.x,
  227.                                                    (SHORT) ptlMouse.y), NULL) ;
  228.                          break ;
  229.  
  230.                     default:
  231.                          WinMapWindowPoints (hwnd, HWND_DESKTOP, &ptlMouse, 1) ;
  232.                          WinSetPointerPos (HWND_DESKTOP, (SHORT) ptlMouse.x,
  233.                                                          (SHORT) ptlMouse.y) ;
  234.                          break ;
  235.                     }
  236.                return 0 ;
  237.  
  238.           case WM_JUDGE_SAYS_MOVE_PIECE:
  239.                sTimerBeg   = SHORT1FROMMP (mp1) ;
  240.                sTimerEnd   = SHORT2FROMMP (mp1) ;
  241.                sTimerColor = SHORT1FROMMP (mp2) ;
  242.                sTimerKing  = SHORT2FROMMP (mp2) ;
  243.                sTimerInc   = 0 ;
  244.  
  245.                WinStartTimer (hab, hwnd, TIMER_ID, TIMER_TIME) ;
  246.                return 0 ;
  247.  
  248.           case WM_TIMER:
  249.                if (sTimerInc == 0)
  250.                     {
  251.                     CkdConvertIndexToCoords (sTimerBeg, &x, &y, sBottom) ;
  252.                     CkdErasePiece (hps, x, y) ;
  253.                     CkdExternalSave (hps, x, y, sTimerKing) ;
  254.                     CkdExternalShow (hps, x, y, sTimerColor, sTimerKing) ;
  255.                     }
  256.  
  257.                CkdExternalMove (hps, sTimerBeg,  sTimerEnd, sTimerColor,
  258.                                      sTimerKing, sBottom,   sTimerInc,
  259.                                      TIMER_STEPS) ;
  260.  
  261.                if (++sTimerInc >= TIMER_STEPS)
  262.                     {
  263.                     CkdConvertIndexToCoords (sTimerEnd, &x, &y, sBottom) ;
  264.                     CkdExternalRestore (hps, x, y, sTimerKing) ;
  265.                     CkdDragDeposit (hps, x, y, sTimerColor, sTimerKing) ;
  266.                     WinStopTimer (hab, hwnd, TIMER_ID) ;
  267.  
  268.                     WinSendMsg (hwndJudge, WM_TELL_JUDGE_PIECE_MOVED,
  269.                                 NULL, NULL) ;
  270.                     }
  271.                return 0 ;
  272.                                    // need to king piece if new king !
  273.  
  274.  
  275.           case WM_JUDGE_SAYS_REMOVE_PIECE:
  276.                i = SHORT1FROMMP (mp1) ;
  277.                CkdConvertIndexToCoords (i, &x, &y, sBottom) ;
  278.                CkdErasePiece (hps, x, y) ;
  279.                return 0 ;
  280.  
  281.           case WM_JUDGE_SAYS_KING_PIECE:
  282.                i = SHORT1FROMMP (mp1) ;
  283.                CkdConvertIndexToCoords (i, &x, &y, sBottom) ;
  284.                CkdErasePiece (hps, x, y) ;
  285.                CkdDragDeposit (hps, x, y, sTimerColor, 1) ;
  286.                return 0 ;
  287.  
  288.           case WM_TELL_BOARD_COLOR_DIALOG:
  289.                if (!WinDlgBox (HWND_DESKTOP, hwnd, ColorDlgProc,
  290.                                0, IDD_COLOR_DLG, mp1))
  291.                     return 0 ;
  292.  
  293.                if (SHORT1FROMMP (mp2))
  294.                     {
  295.                     CkdDestroyPieces () ;
  296.                     CkdCreatePieces (hps) ;
  297.                     }
  298.                WinInvalidateRect (hwnd, NULL, FALSE) ;
  299.                return 0 ;
  300.  
  301.           case WM_TELL_BOARD_STANDARD_COLORS:
  302.                CkdSetStandardColors () ;
  303.                CkdDestroyPieces () ;
  304.                CkdCreatePieces (hps) ;
  305.                WinInvalidateRect (hwnd, NULL, FALSE) ;
  306.                return 0 ;
  307.  
  308.           case WM_SETFOCUS:
  309.                               // set the mouse pointer
  310.  
  311.                if (WinQuerySysValue (HWND_DESKTOP, SV_MOUSEPRESENT) == 0)
  312.                     WinShowPointer (HWND_DESKTOP,
  313.                                     SHORT1FROMMP (mp2) ? TRUE : FALSE) ;
  314.  
  315.                WinSetPointer (HWND_DESKTOP, hptr) ;
  316.                return 0 ;
  317.  
  318.           case WM_PAINT:
  319.                WinBeginPaint (hwnd, hps, NULL) ;
  320.  
  321.                WinSendMsg (hwndJudge, WM_QUERY_JUDGE_CURRENT_BOARD,
  322.                            MPFROMP (&brd), NULL) ;
  323.  
  324.                CkdDrawWindowBackground (hps, hwnd) ;
  325.                CkdDrawWholeBoard (hps) ;
  326.                CkdDrawAllPieces (hps, &brd, sBottom) ;
  327.  
  328.                if (fMovingPiece)
  329.                     {
  330.                     WinQueryPointerPos (HWND_DESKTOP, &ptlMouse) ;
  331.                     WinMapWindowPoints (HWND_DESKTOP, hwnd, &ptlMouse, 1) ;
  332.                     CkdDragSave (hps, &ptlMouse, sKing) ;
  333.                     CkdDragShow (hps, &ptlMouse, sColor, sKing) ;
  334.  
  335.                     ptlLast = ptlMouse ;
  336.                     }
  337.  
  338.                WinEndPaint (hps) ;
  339.                return 0 ;
  340.  
  341.           case WM_DESTROY:
  342.                CkdDestroyPieces () ;
  343.                CkdDestroyPS (hps) ;
  344.                return 0 ;
  345.           }
  346.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  347.      }
  348.